#ifndef __SYS_H
#define __SYS_H
#include "stm32f1xx_ll_dac.h"
#include "stm32f1xx_ll_dma.h"
#include "stm32f1xx_ll_rcc.h"
#include "stm32f1xx_ll_bus.h"
#include "stm32f1xx_ll_system.h"
#include "stm32f1xx_ll_exti.h"
#include "stm32f1xx_ll_cortex.h"
#include "stm32f1xx_ll_utils.h"
#include "stm32f1xx_ll_pwr.h"
#include "stm32f1xx_ll_spi.h"
#include "stm32f1xx_ll_tim.h"
#include "stm32f1xx_ll_usart.h"
#include "stm32f1xx_ll_i2c.h"
#include "stm32f1xx_ll_gpio.h"
#include "stm32f1xx.h"
#include "math.h"
#include "SEGGER_RTT.h"

//位带访问原理:                                 基地址        上移32M字节    字节偏移*32      位偏移 * 4
#define BIT_ADDR(addr, bit) ((addr & 0xF0000000) + 0x2000000 + ((addr & 0xFFFFF) << 5) + (bit << 2))
#define BIT(addr, bit) *((volatile uint32_t *)(BIT_ADDR(addr, bit)))
#define ODR_OFFSET 12
#define IDR_OFFSET 8
#define PAout(n)                            BIT((GPIOA_BASE + ODR_OFFSET), n)
#define PBout(n)                            BIT((GPIOB_BASE + ODR_OFFSET), n)
#define PCout(n)                            BIT((GPIOC_BASE + ODR_OFFSET), n)
#define PDout(n)                            BIT((GPIOD_BASE + ODR_OFFSET), n)
#define PEout(n)                            BIT((GPIOE_BASE + ODR_OFFSET), n)
#define PFout(n)                            BIT((GPIOF_BASE + ODR_OFFSET), n)
#define PGout(n)                            BIT((GPIOG_BASE + ODR_OFFSET), n)
#define PAin(n)                             BIT((GPIOA_BASE + IDR_OFFSET), n)
#define PBin(n)                             BIT((GPIOB_BASE + IDR_OFFSET), n)
#define PCin(n)                             BIT((GPIOC_BASE + IDR_OFFSET), n)
#define PDin(n)                             BIT((GPIOD_BASE + IDR_OFFSET), n)
#define PEin(n)                             BIT((GPIOE_BASE + IDR_OFFSET), n)
#define PFin(n)                             BIT((GPIOF_BASE + IDR_OFFSET), n)
#define PGin(n)                             BIT((GPIOG_BASE + IDR_OFFSET), n)

#ifndef NVIC_PRIORITYGROUP_0
#define NVIC_PRIORITYGROUP_0                ((uint32_t)0x00000007) /*!< 0 bit  for pre-emption priority,4 bits for subpriority */
#define NVIC_PRIORITYGROUP_1                ((uint32_t)0x00000006) /*!< 1 bit  for pre-emption priority,3 bits for subpriority */
#define NVIC_PRIORITYGROUP_2                ((uint32_t)0x00000005) /*!< 2 bits for pre-emption priority,2 bits for subpriority */
#define NVIC_PRIORITYGROUP_3                ((uint32_t)0x00000004) /*!< 3 bits for pre-emption priority,1 bit  for subpriority */
#define NVIC_PRIORITYGROUP_4                ((uint32_t)0x00000003) /*!< 4 bits for pre-emption priority,0 bit  for subpriority */
#endif

void Delay_us_with_Break_by_Function(uint32_t nus, uint8_t(*fun(void)));
void Delay_us_With_Break_by_Value(uint32_t nus, volatile uint8_t *pValue);
void Delay_us(uint32_t nus);
void GPIO_DeInit();
void MCU_Standby();
void SystemClockConfigHSE(void);
void SystemClockConfigHSI(void);
void SystemPoweronInitWithHSE(void);
void SystemPoweronInitWithHSI(void);

typedef struct
{
    GPIO_TypeDef *GPIOx;            // 指向哪个IO口
    LL_GPIO_InitTypeDef GPIO_Param; // 初始化参数
} GPIO_Init_Param_Typedef;

#ifdef DEBUG
    #define LOG SEGGER_RTT_printf
#else
    #define LOG
#endif

#endif
